#!/bin/tclsh

#### auto selected
proc auto_select {} {
    set blist [dbget selected]

    deselectAll

    foreach bump $blist {

    set box [lindex [dbget $bump.bump_shape_bbox] 0]
    set box_size [dbget $bump.bump_shape_bbox_size]

    # down
    while true {
        set isBump [dbQuery -areas $box -objType bump]
        if { $isBump != "0x0" } {
            select_obj $isBump
        }
        
        set box "[lindex $box 0] [expr [lindex $box 1]-20] [lindex $box 2] [expr [lindex $box 3]-20]"
        
        if { [lindex $box 1] < 0 } {
            break
        }
    }
}

#### change bump name in list
proc pr_change_BumpName { PG } {
    set alist [dbget selected]

    foreach bump ${alist} {
        deselectAll
        select_obj $bump
        set bump_name [dbget $bump.name]
        edit_bump_name -from $bump_name -to [format "%s_%s" ${bump_name} $PG] -selected
    }
}

#### fix bug
proc pr_fix_bug {} {
if { 1 } {
    set alist [dbget selected]
    foreach bump ${alist} {
        deselectAll
        select_obj $bump
        set bump_name [dbget $bump.name]
        puts $bump_name
        set list_str [split $bump_name "_"]
        #edit_bump_name -from $bump_name -to [format "%s_%s_%s" [lindex $list_str 0] [lindex $list_str 1] [lindex $list_str 2]] -selected
        edit_bump_name -from $bump_name -to [format "%s_%s" [lindex $list_str 0] [lindex $list_str 1]] -selected
    }
}
}

#### change signal net 
proc pr_changeSignalNet { signal_net } {
#    if {$argc != 1} {
#        puts "Usage: xx.tcl signal_net"
#        puts "signal_net : String type"
#        exit 1
#    }

    set bump [dbget selected]
    set bump_name [dbget $bump.name]
    edit_bump_name -from $bump_name -to [format "%s_%s" $bump_name $signal_net] -selected
}


#### sort ND signal bump
proc pr_sort_NDsignalBump { number ori type} {
    if { $ori == "H"} {
        array set myDict {}
        set alist [list ]
        set selectList [dbget selected]
        foreach bump $selectList {
            set x [dbget $bump.pt_x]
            lappend alist $x
            set myDict($x) $bump
        }
        set sort_alist [lsort $alist]
        puts $sort_alist

        if { $type == "IO" } {
            set IO_list [list "AD0_CH" "AD1_CH" "AD2_CH" "AD3_CH" "DQS_C_CH" "DQS_CH" "AD4_CH" "AD5_CH" "AD6_CH" "AD7_CH" "DBI_CH"]
            set i 0
            foreach io_net $IO_list {
                set name [format "%s%s" $io_net $number]
                puts $name
                set local_bump $myDict([lindex $sort_alist $i])
                puts $local_bump
                deselectAll
                select_obj $local_bump
                set bump [dbget selected]
                set bump_name [dbget $bump.name]
                edit_bump_name -from $bump_name -to [format "%s_%s" $bump_name $name] -selected
                incr i
            }
        } elseif { $type == "CM" } {
            set CM_list [list "RE_CH" "RE_C_CH" "ALE_CH" "CLE_CH" "WE_CH" "CE4_CH" "CE5_CH" "CE6_CH" "CE7_CH" "CE0_CH" "CE1_CH" "CE2_CH" "CH3_CH"]
            set i 0
            foreach io_net $CM_list {
                set name [format "%s%s" $io_net $number]
                puts $name
                set local_bump $myDict([lindex $sort_alist $i])
                puts $local_bump
                deselectAll
                select_obj $local_bump
                set bump [dbget selected]
                set bump_name [dbget $bump.name]
                edit_bump_name -from $bump_name -to [format "%s_%s" $bump_name $name] -selected
                incr i
            }
        }
    } elseif { $ori == "V" } {
        array set myDict {}
        set alist [list ]
        set selectList [dbget selected]
        foreach bump $selectList {
            set x [dbget $bump.pt_y]
            lappend alist $x
            set myDict($x) $bump
        }
        set sort_alist [lsort $alist]
        puts $sort_alist

        if { $type == "IO" } {
            set IO_list [list "AD0_CH" "AD1_CH" "AD2_CH" "AD3_CH" "DQS_C_CH" "DQS_CH" "AD4_CH" "AD5_CH" "AD6_CH" "AD7_CH" "DBI_CH"]
            set i 0
            foreach io_net $IO_list {
                set name [format "%s%s" $io_net $number]
                puts $name
                set local_bump $myDict([lindex $sort_alist $i])
                puts $local_bump
                deselectAll
                select_obj $local_bump
                set bump [dbget selected]
                set bump_name [dbget $bump.name]
                edit_bump_name -from $bump_name -to [format "%s_%s" $bump_name $name] -selected
                incr i
            }
        } elseif { $type == "CM" } {
            set CM_list [list "RE_CH" "RE_C_CH" "ALE_CH" "CLE_CH" "WE_CH" "CE4_CH" "CE5_CH" "CE6_CH" "CE7_CH" "CE0_CH" "CE1_CH" "CE2_CH" "CH3_CH"]
            set i 0
            foreach io_net $CM_list {
                set name [format "%s%s" $io_net $number]
                puts $name
                set local_bump $myDict([lindex $sort_alist $i])
                puts $local_bump
                deselectAll
                select_obj $local_bump
                set bump [dbget selected]
                set bump_name [dbget $bump.name]
                edit_bump_name -from $bump_name -to [format "%s_%s" $bump_name $name] -selected
                incr i
            }
        }
    }
}

#### sort DDR signal bump
proc pr_sort_DDRsignalBump {ori} {
    set IO_list [list DDR_CH2_DQ31 DDR_CH2_DQ30 DDR_CH2_DQ29 DDR_CH2_DQ28 DDR_CH2_DQ27 DDR_CH2_DQS3 DDR_CH2_DQSN3 DDR_CH2_WCK3 DDR_CH2_WCKN3 DDR_CH2_DQ26 DDR_CH2_DQ25 DDR_CH2_DQ24 DDR_CH2_DM3]
    #set IO_list [lreverse $a_list]

    if { $ori == "H"} {
        array set myDict {}
        set alist [list ]
        set selectList [dbget selected]
        foreach bump $selectList {
            set x [dbget $bump.pt_x]
            lappend alist $x
            set myDict($x) $bump
        }
        set sort_alist [lsort $alist]
        puts $sort_alist

        set i 0
        foreach io_net $IO_list {
            set name $io_net
            puts $name
            set local_bump $myDict([lindex $sort_alist $i])
            puts $local_bump
            deselectAll
            select_obj $local_bump
            set bump [dbget selected]
            set bump_name [dbget $bump.name]
            edit_bump_name -from $bump_name -to [format "%s_%s" $bump_name $name] -selected
            incr i
        }
    } elseif { $ori == "V" } {
        array set myDict {}
        set alist [list ]
        set selectList [dbget selected]
        foreach bump $selectList {
            set x [dbget $bump.pt_y]
            lappend alist $x
            set myDict($x) $bump
        }
        set sort_alist [lsort $alist]
        puts $sort_alist

        set i 0
        foreach io_net $IO_list {
            set name $io_net
            puts $name
            set local_bump $myDict([lindex $sort_alist $i])
            puts $local_bump
            deselectAll
            select_obj $local_bump
            set bump [dbget selected]
            set bump_name [dbget $bump.name]
            edit_bump_name -from $bump_name -to [format "%s_%s" $bump_name $name] -selected
            incr i
        }
    }
}